package com.sunwayworld.basemodule.coredata.method.service.impl;

import com.sunwayworld.basemodule.coredata.method.bean.LimsMethodCertificateBean;
import com.sunwayworld.basemodule.coredata.method.service.LimsMethodCertificateService;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.context.LocalContextHelper;
import com.sunwayworld.framework.exception.InvalidDataException;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.support.PersistableMetadataHelper;
import com.sunwayworld.framework.utils.CollectionUtils;
import com.sunwayworld.framework.utils.StringUtils;
import com.sunwayworld.module.item.file.bean.CoreFileBean;
import com.sunwayworld.module.item.file.service.CoreFileService;
import com.sunwayworld.module.sys.bpmn.bean.CoreBpmnCommentBean;
import com.sunwayworld.module.sys.bpmn.service.CoreBpmnCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.coredata.method.dao.LimsMethodCertificateUserDao;
import com.sunwayworld.basemodule.coredata.method.bean.LimsMethodCertificateUserBean;
import com.sunwayworld.basemodule.coredata.method.service.LimsMethodCertificateUserService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsMethodCertificateUserServiceImpl implements LimsMethodCertificateUserService {

    @Autowired
    private LimsMethodCertificateUserDao limsMethodCertificateUserDao;
    @Autowired
    private CoreFileService coreFileService;
    @Autowired
    private LimsMethodCertificateService limsMethodCertificateService;
    @Autowired
    @Lazy
    private CoreBpmnCommentService coreBpmnCommentService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsMethodCertificateUserDao getDao() {
        return limsMethodCertificateUserDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsMethodCertificateUserBean limsMethodCertificateUser = jsonWrapper.parseUnique(LimsMethodCertificateUserBean.class);
        limsMethodCertificateUser.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsMethodCertificateUser);

        CoreBpmnCommentBean commentBean = new CoreBpmnCommentBean();
        commentBean.setId(ApplicationContextHelper.getNextIdentity());
        commentBean.setTargetId(this.getDao().getTable().concat("$").concat(limsMethodCertificateUser.getId().toString()));
        commentBean.setTaskName("新建");
        commentBean.setRoute("新建");
        commentBean.setUserId(LocalContextHelper.getLoginUserId());
        commentBean.setUserName(LocalContextHelper.getLoginUserName());
        coreBpmnCommentService.getDao().insert(commentBean);

        return limsMethodCertificateUser.getId();
    }

    @Transactional
    @AuditTrailEntry(AuditTrailType.DELETE)
    @Audit("AUDIT.DELETE")
    @Override
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsMethodCertificateUserBean> deleteList = wrapper.parse(this.getDao().getType());
        List<Long> idList = deleteList.stream().map(LimsMethodCertificateUserBean::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(idList)){
            List<LimsMethodCertificateUserBean> beanList = this.selectListByIds(idList);
            List<LimsMethodCertificateUserBean> draft = beanList.stream().filter(item -> !StringUtils.startsWith(item.getProcessStatus(), "draft")).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(draft)){
                throw new InvalidDataException(I18nHelper.getMessage("AUDITING_WORKFLOW.TIP.DELETE_NOT"));  //流程中禁止删除
            }
            //查询意向跟进
            List<LimsMethodCertificateBean> followupBeans=limsMethodCertificateService.selectListByFilter(
                    SearchFilter.instance().match("personId",idList).filter(MatchPattern.OR));

            String tableName = PersistableMetadataHelper.getTableName(LimsMethodCertificateUserBean.class);

            List<CoreFileBean> coreFileBeans = new ArrayList<>();
            beanList.forEach(t ->{
                CoreFileBean coreFileBean = new CoreFileBean();
                coreFileBean.setTargetId(tableName.toUpperCase() + "$" + t.getId());
                coreFileBeans.add(coreFileBean);
            });
            getDao().deleteByIdList(idList);                                               //删除业务表数据
            limsMethodCertificateService.getDao().deleteBy(followupBeans);
            coreFileService.getDao().deleteBy(coreFileBeans, "TARGETID"); //删除附件
        }
    }

}
